# Contributor: David Huffman <storedbox@outlook.com>
# Maintainer: psykose <alice@ayaya.dev>
pkgname=libtbb
pkgver=2021.5.0
pkgrel=1
pkgdesc="oneAPI Threading Building Blocks"
url="https://github.com/oneapi-src/oneTBB"
arch="all"
license="GPL-2.0-only"
depends_dev="linux-headers"
makedepends="
	$depends_dev
	cmake
	hwloc-dev
	libucontext-dev
	ninja
	python3-dev
	swig
	"
subpackages="py3-tbb:_py3 $pkgname-dev $pkgname-doc"
source="tbb-$pkgver.tar.gz::https://github.com/oneapi-src/oneTBB/archive/v$pkgver.tar.gz
	fix-dynload.patch
	fix-underlinking.patch
	fix-python-install.patch
	fix-s390x-arch.patch
	fix-x86-waitpkg.patch
	fix-pthread-create.patch
	tbb.pc
	tbbmalloc.pc
	tbbmalloc_proxy.pc
	"
builddir="$srcdir/oneTBB-$pkgver"

case "$CARCH" in
armhf)
	# tests take hours to run
	options="$options !check"
	;;
esac

prepare() {
	default_prepare

	sed -e "s@#define MALLOC_UNIXLIKE_OVERLOAD_ENABLED __linux__@@"  \
		-i src/tbbmalloc_proxy/proxy.h

	sed -i "s|@@VERSION@@|$pkgver|" -i "$srcdir"/*.pc
}

build() {
	cmake -B build -G Ninja \
		-DCMAKE_BUILD_TYPE=MinSizeRel \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=lib \
		-DCMAKE_VERBOSE_MAKEFILE=1 \
		-DTBB4PY_BUILD=ON \
		-DTBB_STRICT=OFF \
		-DTBB_TEST="$(want_check && echo ON || echo OFF)"
	cmake --build build
}

check() {
	# hang on every arch
	local skip="test_collaborative_call_once|test_eh_flow_graph|test_eh_algorithms"
	skip="$skip|test_flow_graph_priorities|conformance_parallel_pipeline"
	# flaky hang
	skip="$skip|task_group|test_parallel_reduce"
	# flaky segfault
	skip="$skip|test_eh_thread"
	# SIGFPE
	skip="$skip|malloc_whitebox"
	# fails to load module
	skip="$skip|python_test"

	case "$CARCH" in
	armv7|s390x)
		# single failing testcase with sigsegv
		skip="$skip|test_task|test_resumable_tasks|conformance_resumable_tasks"
		;;
	esac

	export CTEST_PARALLEL_LEVEL=4 # high threads fail
	ctest --test-dir build --output-on-failure --exclude-regex "$skip"
}

package() {
	DESTDIR="$pkgdir" cmake --install build

	install -Dm0644 "$srcdir"/*.pc -t "$pkgdir"/usr/lib/pkgconfig
}

_py3() {
	pkgdesc="$pkgdesc (python bindings)"
	amove usr/lib/python3.*
}

sha512sums="
0e7b71022e397a6d7abb0cea106847935ae79a1e12a6976f8d038668c6eca8775ed971202c5bd518f7e517092b67af805cc5feb04b5c3a40e9fbf972cc703a46  tbb-2021.5.0.tar.gz
33caa88a26fae793f052b659ffccd5de21ebc813ec30a10393d07f28f6a579d7aa5f2ce0f758dd341dd9db270a0f2b29928b73d7c49d27a2bf3d5bde4cce69d0  fix-dynload.patch
22a07bf83184ec4edfe33038c328f61446701f7fe1e1998f4e92d564c8ea09d1a2cd99fc4b959b9f6b9e5507febb9696ebd153d5654f2e59a996f0c29bdf304c  fix-underlinking.patch
728d0fcecb91e3262deb1b7acb29ccd58ffda9f502d182048d0ca80826eae20cbda76d365e72ca7f03375d44e60d43edcd07a3b95bfb099c72d8799972764de5  fix-python-install.patch
dfba52da2bd6f463adf658e95a41912c60f12f33b3cdc8e15097dd88c0306fb09c8e269862fda74bbb07cd5091491f611086fb011c79ca762f2be8521cb2fc0b  fix-s390x-arch.patch
cd36975c99fe6d33f3005acc302771d3d5aba6af17fa3d374021f9dbe9d2facf1706d010f3a52673d98606abe47eb623da5df4c1e76f2153339a96726eb835ff  fix-x86-waitpkg.patch
8f8f7f8d08c570af2339d19114b1679ffceee7b0d9b8c22ed12ee3c2ab858684e4cbb498bf7e31966db6b57b5685d46fab312f95972a6e501f6a5781137e799e  fix-pthread-create.patch
b7849e84c5f4d868ad19dc4a2433492facd1f75fe6e5cb0afc334e8f07bb4657d318ee1bc53f7890bfb8a87c7233bba85b61aba2aec9ea3dc24652a4bdec4dde  tbb.pc
25b0bfe2067f287064f81dc434ac70336ad42fdd7fa91cded2e467f3939000480e1012675dc4e435a1712cf9b968cb565f213dfffb3f69ad46929aec237b9a02  tbbmalloc.pc
5952862755736fb85982281b3953183ed9d4657497a7f6bae1ef66bd1e2265882773764f07e46679b2aa750f80f47cc2a019024822ab5fa272b718c539ef3462  tbbmalloc_proxy.pc
"
